#include "delegate.h"
#include "constants.h"
#include "tools.h"

#include <QComboBox>
#include <QDateEdit>


using namespace Constants;
using namespace ToolsSpace;

ComboDelegate::ComboDelegate(QObject * parent)
 {
     Q_UNUSED(parent);
     Tools t(this);
     m_hashConditions=t.hashOfConditionDelegateCombo();
     m_hashTypesOfDocuments = t.hashIntAndTypeStringsOfHeads();
     m_hashConjonctions = t.hashOfConjonctions();
 }

ComboDelegate::~ComboDelegate(){}

QSize ComboDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,25);
}

QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
   if ( index.column() == CONDITIONOFRESEARCH || index.column() == TYPEOFDOC || index.column() == CONJONCTION )
    {
   	  QComboBox * combo = new QComboBox(parent);
    	  return combo;
        }
   return QStyledItemDelegate::createEditor(parent, option, index);
}

 void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     combo->setEditable(false);
     for (int row = 0; row < ConditionsOfResearchMaxParam; ++row )
     {
     	  if (index.column() == CONDITIONOFRESEARCH)
     	  {
     	        combo->addItem(m_hashConditions.value(row));
     	      }
     	  if (index.column() == TYPEOFDOC)
     	  {
     	        combo->addItem(m_hashTypesOfDocuments.value(row));
     	      }
         }
     if (index.column() == CONJONCTION)
     	  {
     	        for (int i = 0; i < Conjonctions_MaxParam; ++i)
     	        {
     	              combo->addItem(m_hashConjonctions.value(i));
     	            }
     	      }
     QVariant cond = index.model()->data(index,Qt::DisplayRole);
     if (index.column() == CONDITIONOFRESEARCH)
     {
           combo->setCurrentIndex(m_hashConditions.key(cond.toString()));
         }
     if (index.column() == TYPEOFDOC)
     {
            combo->setCurrentIndex(m_hashTypesOfDocuments.key(cond.toString()));
         }
     if (index.column() == CONJONCTION)
     {
           combo->setCurrentIndex(m_hashConjonctions.key(cond.toString()));
         }
     
     
}
 
 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     int row = combo->currentIndex();
     QVariant value ;
     if (index.column() == CONDITIONOFRESEARCH)
     {
        switch(row){
           case CONTAINS :
             value = m_hashConditions.value(CONTAINS);
             break;
           case INF :
             value = m_hashConditions.value(INF);
             break;
           case SUP :
             value = m_hashConditions.value(SUP);
             break;
           default :
             break;    
         } 
         }
      if (index.column() == TYPEOFDOC)
      {
            switch(row){
                case OBSERVATION :
                    value = m_hashTypesOfDocuments.value(OBSERVATION);
                    break;
                case DOCUMENTS :
                    value = m_hashTypesOfDocuments.value(DOCUMENTS);
                    break;
                case ORDONNANCE_AJUSTABLE :
                    value = m_hashTypesOfDocuments.value(ORDONNANCE_AJUSTABLE);
                    break;
                case TERRAIN :
                    value = m_hashTypesOfDocuments.value(TERRAIN);
                default :
                    break;    
                }          
            }
        if (index.column() == CONJONCTION)
        {
              switch(row){
                  case AND :
                      value = m_hashConjonctions.value(AND);
                      break;
                  case OR :
                      value = m_hashConjonctions.value(OR);
                      break;
                  default :
                      break;    
                  }             
                  
            }
            

      model->setData(index, value, Qt::EditRole);      
 }
 
 //////////////////////////////////////////////////////////
 //////////////DATE EDIT//////////////
 //////////////////////////////////////////
 
 DateDelegate::DateDelegate(QObject * parent)
 {
     Q_UNUSED(parent);
 }

DateDelegate::~DateDelegate(){}

QSize DateDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,25);
}

QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
   if (  index.column() == DATEBEGIN || index.column() == DATEEND)
    {
    	  QDateEdit * dateEdit = new QDateEdit(parent);
    	  dateEdit->setDisplayFormat("yyyy-MM-dd");
    	  return dateEdit;
        }
   return QStyledItemDelegate::createEditor(parent, option, index);
}

 void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
     QDate date;
     date = index.model()->data(index,Qt::DisplayRole).toDate();
     dateEdit->setDate(date);
}
 
 void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
 {
     QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
     QString value = dateEdit->date().toString("yyyy-MM-dd");

     model->setData(index, value, Qt::EditRole);
 }
 
 //////////////////////////////////////////////////////////////////////////
 //////////TOKEN TERRAIN//////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////
 
 TokenTerrainDelegate::TokenTerrainDelegate(QObject * parent)
 {
     Q_UNUSED(parent);
     Tools t(this);
     m_hashOfTokens = t.hashOfTokens();
 }

TokenTerrainDelegate::~TokenTerrainDelegate(){}

QSize TokenTerrainDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,25);
}

QWidget *TokenTerrainDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
   if ( index.column() == TOKENEDIT )
    {
   	  QComboBox * combo = new QComboBox(parent);
    	  return combo;
        }
   return QStyledItemDelegate::createEditor(parent, option, index);
}

 void TokenTerrainDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     combo->setEditable(false);
     for (int row = 0; row < m_hashOfTokens.size(); ++row )
     {
     	  combo->addItem(m_hashOfTokens.value(row));
         }
}
 
 void TokenTerrainDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     QString value = combo->currentText();
     model->setData(index, value, Qt::EditRole);      
 }
 
 ///////////////////////////////////////////////////////////////
 /////////////combobox of indexes of value to compare in the line of document
 ////////////////////////////////////////////////////////////////////////////////
 
 IndexOfValueInLineDelegate::IndexOfValueInLineDelegate(QObject * parent)
 {
     Q_UNUSED(parent);
     Tools t(this);
     m_hashIndexes.insert(0,"auto");
     for ( int i = 1; i < 11; ++ i)
     {
           m_hashIndexes.insert(i,QString::number(i));
         }
 }

IndexOfValueInLineDelegate::~IndexOfValueInLineDelegate(){}

QSize IndexOfValueInLineDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,25);
}

QWidget *IndexOfValueInLineDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
   if ( index.column() == INDEXOFVALUE )
    {
   	  QComboBox * combo = new QComboBox(parent);
    	  return combo;
        }
   return QStyledItemDelegate::createEditor(parent, option, index);
}

 void IndexOfValueInLineDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     combo->setEditable(false);
     for (int row = 0; row < m_hashIndexes.size(); ++row )
     {
     	  combo->addItem(m_hashIndexes.value(row));
         }     
}
 
 void IndexOfValueInLineDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     QString value = combo->currentText();
     model->setData(index, value, Qt::EditRole);      
 }
